<?php namespace App\Modules\Admin\Controllers;

use App\Http\Requests;
use App\Models\Action;
use App\Models\Permission;
use App\Models\Role;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use App\Modules\Admin\Controllers\Controller;

/**
 * Permissions controller
 *
 * @author Todor Todorov <todstoychev@gmail.com>
 * @package App\Http\Controllers\Admin
 */
class AdminPermissionsController extends Controller
{

    /**
     * define action names
     * @var array
     */
    public static $actionNames = [
        'getAll'=>'访问权限列表',
        'putAll'=>'修改权限信息',
        'getSearch'=>'访问权限搜索结果',
        'postSearch'=>'执行权限搜索',
    ];

    /**
     * Get all page
     *
     * @return \Illuminate\View\View
     */
    public function getAll(Request $request)
    {
        $query = Action::with(['permissions', 'permissions.role'])->where('name','<>','\'\'');
        $roles = Role::all();

        $queryData = $this->getQueryData($request, $query,  'admin/permissions', trans('permissions.all_title'), "");

        $data = array_merge($queryData, array('roles'=>$roles));

        return $this->render('permissions.all', $data);
    }

    /**
     * Handle permissions change
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function putAll(Request $request) {
        $input = array_keys($request->input('permissions'));
        $permissionIds = explode(',', $request->input('permission_ids'));

        $permissions = Permission::whereIn('id', $permissionIds)->get();
        try {
            DB::beginTransaction();
            $permissions->each(function ($permission) use ($input) {
                if (in_array($permission->id, $input)) {
                    $permission->allow = true;
                } else {
                    $permission->allow = false;
                }
                $permission->save();
            });
            DB::commit();

            flash()->success(trans('permissions.save_success'));
        } catch (\Exception $e) {
            flash()->error(trans('permissions.save_error'));
        }

        try {
            Cache::tags(['permissions'])->flush();
        } catch (\Exception $e) {
            Cache::flush();
        }

        return redirect()->back();
    }

    /**
     * Handle search page
     *
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function getSearch(Request $request)
    {
        $search = $request->input('search');

        $query = Action::search($search)->where('name','<>','\'\'');
        $roles = Role::all();

        $queryData = $this->getQueryData($request, $query,  'admin/permissions', trans('permissions.all_title'), "");

        $data = array_merge($queryData, array('roles'=>$roles));

        return $this->render('permissions.all', $data);
    }

    /**
     * Handle search form
     *
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function postSearch(Request $request)
    {
        return $this->getSearch($request);
    }
}
